<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="设备名称" prop="device">
        <el-input
          v-model="queryParams.device"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status">
          <el-option
            v-for="(item, index) in Dictionary.classifyPlanStatus"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="联系方式" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="业主公司" prop="ownerCompanyId">
        <el-select v-model="queryParams.ownerCompanyId" filterable clearable>
          <el-option
            v-for="(item, index) in componentList"
            :key="index"
            :label="item.entName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间"
        >
        </el-date-picker>
        <!-- <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入开始时间"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间"
        >
        </el-date-picker>
        <!-- <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入结束时间"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>

      <!-- <el-form-item label="用户">
        <el-select v-model="queryParams.applyerId">
          <el-select v-model="queryParams.ownerCompanyId">
          <el-option v-for="item,index in componentList" :key="index" :label="item"></el-option>
        </el-select>
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="预约上门时间" prop="subscribeDate">
        <el-date-picker clearable
          v-model="queryParams.subscribeDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预约上门时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="业主公司id" prop="ownerCompanyId">
        <el-input
          v-model="queryParams.ownerCompanyId"
          placeholder="请输入业主公司id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业主公司名称" prop="ownerCompanyName">
        <el-input
          v-model="queryParams.ownerCompanyName"
          placeholder="请输入业主公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="申请用户id" prop="applyerId">
        <el-input
          v-model="queryParams.applyerId"
          placeholder="请输入申请用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请用户名称" prop="applyerName">
        <el-input
          v-model="queryParams.applyerName"
          placeholder="请输入申请用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wb:wbRepairApply:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wb:wbRepairApply:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['wb:wbRepairApply:export']">导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="wbRepairApplyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column label="设备名称" align="center" prop="device" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="联系方式" align="center" prop="phoneNumber" />
      <el-table-column
        label="预约上门时间"
        align="center"
        prop="subscribeDate"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.subscribeDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <span>{{
            fomatterLabelName("classifyPlanStatus", scope.row.status)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="故障描述" align="center" prop="remark" />
      <el-table-column label="故障图片" align="center" prop="photo" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photo" :width="50" :height="50" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="业主公司id" align="center" prop="ownerCompanyId" /> -->
      <el-table-column
        label="业主公司名称"
        align="center"
        prop="ownerCompanyName"
      />
      <!-- <el-table-column label="申请用户id" align="center" prop="applyerId" /> -->
      <el-table-column label="申请用户名称" align="center" prop="applyerName" />
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            :disabled="scope.row.status != 6"
            @click="handleUpdate(scope.row, 1)"
            v-hasPermi="['wb:wbRepairApply:edit']"
            >分配师傅</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 2)"
            v-hasPermi="['wb:wbRepairApply:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wb:wbRepairApply:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报修申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form
        ref="form"
        :rules="rules"
        label-width="80px"
        v-if="optionType == 1"
        :inline="true"
      >
        <el-form-item label="师傅" prop="device">
          <el-select v-model="masterId" filterable placeholder="请选择师傅">
            <el-option
              v-for="(item, index) in masterList1"
              :key="index"
              :label="item.userName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form
        ref="form"
        :model="form"
        label-width="110px"
        :rules="rules1"
        :inline="true"
        class="dialog-form"
        v-else
      >
        <el-form-item label="设备名称" prop="device" class="form-item-row">
          <el-input
            v-model="form.device"
            clearable
            placeholder="请输入设备名称"
          />
        </el-form-item>
        <el-form-item label="联系方式" prop="phoneNumber">
          <el-input
            v-model="form.phoneNumber"
            clearable
            placeholder="请输入联系方式"
          />
        </el-form-item>
        <el-form-item label="预约上门时间" prop="subscribeDate">
          <el-date-picker
            clearable
            v-model="form.subscribeDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预约上门时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="业主公司" prop="ownerCompanyId">
          <el-select
            v-model="form.ownerCompanyId"
            filterable
            clearable
            placeholder="请选择业务公司"
          >
            <el-option
              v-for="(item, index) in componentList"
              :key="index"
              :label="item.entName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address" class="form-item-row">
          <el-input
            v-model="form.address"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="故障描述" prop="remark" class="form-item-row">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          label="故障图片"
          prop="photo"
          label-width="200px"
          class="row"
        >
          <image-upload v-model="form.photo" />
        </el-form-item>

        <!-- <el-form-item label="申请用户id" prop="applyerId">
          <el-input v-model="form.applyerId" placeholder="请输入申请用户id" />
        </el-form-item>
        <el-form-item label="申请用户名称" prop="applyerName">
          <el-input v-model="form.applyerName" placeholder="请输入申请用户名称" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWbRepairApply,
  getWbRepairApply,
  delWbRepairApply,
  addWbRepairApply,
  updateWbRepairApply,
} from "@/api/wb/wbRepairApply";
import { fomatterLabelName } from "@/utils/index.js";
import { mapGetters, mapActions } from "vuex";

import { listWbMasterEnter, acquiringMaster } from "@/api/wb/wbMasterEnter.js";

import Dictionary from "@/utils/dictionary";
export default {
  name: "WbRepairApply",
  data() {
    return {
      Dictionary,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报修申请表格数据
      wbRepairApplyList: [],
      // 弹出层标题
      title: "",
      masterList1: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        device: null,
        address: null,
        phoneNumber: null,
        subscribeDate: null,
        status: null,
        photo: null,
        ownerCompanyId: null,
        ownerCompanyName: null,
        applyerId: null,
        applyerName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      rules1: {
        device: [
          {
            required: true,
            message: "设备名称不能为空",
            trigger: "blur",
          },
        ],

        phoneNumber: [
          {
            required: true,
            message: "联系方式不能为空",
            trigger: "blur",
          },
        ],

        subscribeDate: [
          {
            required: true,
            message: "预约上门时间不能为空",
            trigger: "blur",
          },
        ],

        ownerCompanyId: [
          {
            required: true,
            message: "业主公司不能为空",
            trigger: "blur",
          },
        ],

        address: [
          {
            required: true,
            message: "地址不能为空",
            trigger: "blur",
          },
        ],

        photo: [
          {
            required: true,
            message: "故障图片不能为空",
            trigger: "blur",
          },
        ],
      },

      // 分配的师傅id
      masterId: "",
      optionType: 2,
    };
  },
  computed: {
    ...mapGetters(["masterList", "componentList"]),
  },
  created() {
    this.getList();

    // 查询所有师傅
    this.getMasterList();
    // 查询所有业主公司
    this.getCompanyList();
    // 查询所有用户
    this.getUseList();
  },
  methods: {
    fomatterLabelName,
    ...mapActions("reqSelect", ["getMasterList", "getCompanyList"]),
    /** 查询报修申请列表 */
    getList() {
      this.loading = true;
      listWbRepairApply(this.queryParams).then((response) => {
        this.wbRepairApplyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getUseList() {},
    // 分配
    allocation(row) {},
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        device: null,
        address: null,
        phoneNumber: null,
        subscribeDate: null,
        status: null,
        remark: null,
        photo: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        ownerCompanyId: null,
        ownerCompanyName: null,
        applyerId: null,
        applyerName: null,
      };
      this.masterId = "";
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报修申请";
      this.optionType = -1;
    },
    /** 修改按钮操作 */
    handleUpdate(row, flag) {
      // bia
      // 1分配    2修改
      this.optionType = flag;
      this.reset();
      const id = row.id || this.ids;
      getWbRepairApply(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报修申请";
      });

      this.getMasterList();
    },

    getMasterList() {
      acquiringMaster().then((res) => {
        this.masterList1 = res.data;
        console.log(this.masterList1, "masterListmasterListmasterList");
      });
    },

    /** 提交按钮 */
    submitForm() {
      if (this.optionType == 1) {
        updateWbRepairApply({
          id: this.form.id,
          status: 1,
          repairerId: this.masterId,
        }).then((res) => {
          this.$modal.msgSuccess("分配成功");
          this.open = false;
          this.getList();
        });
      } else {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.id != null) {
              updateWbRepairApply(this.form).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWbRepairApply(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除报修申请编号为"' + ids + '"的数据项？')
        .then(function () {
          return delWbRepairApply(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "wb/wbRepairApply/export",
        {
          ...this.queryParams,
        },
        `wbRepairApply_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
